<?php
include_once("dbconnect/db.php");
include_once("class/Common.php");
Class Format_start
{
	var $errorMessage;
	
	var $videoId;
	
	var $memberId = "";
	
	// 変換種類
	var $type;
	
	var $carrierId = "";
	
	function mytask()
	{	
		// 初期化の場合
		if(isset($_POST["act"]) == false)
		{
			$common = new Common;
			$common->initCarrierInfo();	
			$this->carrierId = $common->carrierId;
			$this->uId = $common->uId;		
			$this->videoId = $_GET["videoId"];
			$this->categoryId = $_GET["categoryId"];
			$this->getAdvHtml();
			$this->getVideoInfo();
			
			$this->hasThisMember();

			if($this->memberId == "" || $this->memberId == null)
			{
				$this->createNewMember();	
				$this->insertEx_cnt();
			}	
			
			// 変換回数を取得する
			$this->getEx_cnt();
			
			// バッチ処理開始
			// 変換種類を判断する
			// 動画を再生/保存する場合
			if($this->type == 1)
			{
				
			}
			// ｻﾑﾈｲﾙ画像を作る
			else if ($this->type == 2)
			{
				
			}
			// 待ち受け画像を作る
			else if($this->type ==3)
			{
				
			}
			// ﾃﾞｺﾒ素材を作る
			else if($this->type ==4)
			{
				
			}
							
		}
		else if($_POST["act"] == "submit")
		{
			
		}
	}
	
	function initForm()
	{

	}
	
	
	function getSelectSql()
	{
		$sql = "select `entrylist`.`title`,`videolist`.`cid`,`videolist`.`time` ";
		$sql = $sql."from `entrylist`,`videolist` ";
		$sql = $sql."where `videolist`.`video_id` =  ";
		$sql = $sql.$this->videoId." ";
		$sql = $sql."and `videolist`.`site_id` = 1 ";
		$sql = $sql."and `videolist`.`open_flag` = 1 ";
		$sql = $sql."and `entrylist`.`open_flag` = 1 ";
		$sql = $sql."and `videolist`.`cid` is not null ";
		$sql = $sql."and `entrylist`.`entry_id` = `videolist`.`entry_id` ";
		return $sql;
	}
	
	function getAdvSql($id)
	{
		$sql = "select `adspace_id`,`ad_name`,`tag_contents` from `adspace_mst` where `type` = 2 and `open_flag` = 1 and `adspace_id` = ".$id;
		 
		return $sql;
	}	
	
	function getAdvHtml()
	{
		$sql = $this->getAdvSql("1");
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		// 広告が存在の場合
		if($rowsAd > 0)
		{
			$data = mysql_fetch_array($resultAd);
			$this->advHtml1 = $this->advHtml1."<hr color=#3399FF>".$data["ad_name"]."<font size=1>";
			$this->advHtml1 = $this->advHtml1.$data["tag_contents"]."<br clear=all></font>";
		}	
		
		$sql = $this->getAdvSql("2");
		
		$resultAd = mysql_query($sql);
		$rowsAd = mysql_num_rows($resultAd);
		
		// 広告が存在の場合
		if($rowsAd > 0)
		{
			$data = mysql_fetch_array($resultAd);
			$this->advHtml2 = $this->advHtml2."<hr color=#3399FF>".$data["ad_name"]."<font size=1>";
			$this->advHtml2 = $this->advHtml2.$data["tag_contents"]."<br clear=all></font>";
		}
			
	}
	
	function getVideoInfo()
	{
		$sql = $this->getSelectSql();
		
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);

		
		if($rows > 0)
		{		
			$data = mysql_fetch_array($result);
				
			$this->videoHtml = $this->videoHtml."<hr color=#3399FF>".$data["title"]."(".$data["time"]."秒)<br>";
			$this->videoHtml = $this->videoHtml."<img src=http://img.mytube.to/ir/70/http://img.youtube.com/vi/".$data["cid"]."/default.jpg align=left><br><br><br>";
			$this->videoHtml = $this->videoHtml."<a href=format_start.php?type=1>動画を再生/保存する</a><br/>";
			$this->videoHtml = $this->videoHtml."<a href=format_start.php?type=2>ｻﾑﾈｲﾙ画像を作る</a><br/>";
			$this->videoHtml = $this->videoHtml."<a href=format_start.php?type=3>待ち受け画像を作る</a><br/>";
			$this->videoHtml = $this->videoHtml."<a href=format_start.php?type=4>ﾃﾞｺﾒ素材を作る</a><br/>";	
		}
		// データがない場合
		else
		{
			
		}
	}
	
	function hasThisMember()
	{
		
		// ユーザー端末IDに対して、ユーザーIDを取得する
		$sql = "select `memberId`,`type` from memberMst where uim = ".$this->uId;
		
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);

		// データがある場合
		if($rows > 0)
		{
			$data = mysql_fetch_array($result);
			$this->memberId = $data["memberId"];
			$this->user_type = $data["type"];
			
			// 変換総回数を設定する
			// 無料ユーザーの場合
			if($this->user_type == 0)
			{
				$this->ex_total_cnt = 3;
			}
			else if($this->user_type == 1)
			{
				$this->ex_total_cnt = 10;
			}
			else if($this->user_type == 2)
			{
				$this->ex_total_cnt = 30;
			}
			else if($this->user_type == 3)
			{
				$this->ex_total_cnt = 50;
			}	
			
			return true;		
		}
		else
		{
			return false;
		}
		
	}
	
	function createNewMember()
	{
		$sql = "insert into memberMst (`carrierId`,`type`,`uim`,`lastlogin_date`) values (".$this->carrierId;
		$sql = $sql.",0,".$this->uId.",now())";
		
		$result = mysql_query($sql);
		if ($result != "1")
		{
			$this->errorMessage = "システムエラーが発生しました。管理員に連絡してください。";
			return;
		}
		else
		{
			$this->hasThisMember();
		}
	}
	
	// 今日該当ユーザーの変換回数を取得する
	function getEx_cnt()
	{
		$sql = "select ex_cnt from view_history where memberId = ".$this->memberId." and date = ".$this->date;
		
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		
		if($rows > 0)
		{
			$data = mysql_fetch_array($result);
			$this->ex_cnt = $data["ex_cnt"];
		}
		else
		{
			$this->insertEx_cnt();
			$this->ex_cnt = 0;
		}
	}	
	
	function updateEx_cnt()
	{
		$sql = "update view_history set ex_cnt = ex_cnt + 1 where memberId = ".$this->memberId." and date = ".$this->date;
		$result = mysql_query($sql);
		
		
		if($result != "1")
		{
			$this->errorMessage = "システムエラーが発生しました。管理員に連絡してください。";
			return false;
		}
		else 
		{
			return true;
		}
	}
	
	
	function insertEx_cnt()
	{
		$sql = "insert into view_history (date,memberId,ex_cnt) values (".$this->date.",".$this->memberId.",0)";
		$result = mysql_query($sql);
		
		if($result != "1")
		{
			$this->errorMessage = "システムエラーが発生しました。管理員に連絡してください。";
			return false;
		}
		else
		{
			return true;
		}
	}
		
}
?>